package class_object;

public class ConstructorDemo {
    String name;
    int age;
    double salary;

    // 无参构造方法
    public ConstructorDemo() {
        name = "未知";
        age = 0;
        System.out.println("无参构造方法被调用");
    }

    // 有参构造方法
    public ConstructorDemo(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("两个参数的构造方法被调用");
    }

    // 更多参数的构造方法
    public ConstructorDemo(String name, int age, double salary) {
        this(name, age); // 调用本类的其他构造方法
        this.salary = salary;
        System.out.println("三个参数的构造方法被调用");
    }

    void display() {
        System.out.println("姓名：" + name + "，年龄：" + age + "，工资：" + salary);
    }

    public static void main(String[] args) {
        // 调用无参构造方法
        ConstructorDemo obj1 = new ConstructorDemo();
        obj1.display();

        // 调用两个参数的构造方法
        ConstructorDemo obj2 = new ConstructorDemo("张三", 25);
        obj2.display();

        // 调用三个参数的构造方法
        ConstructorDemo obj3 = new ConstructorDemo("李四", 30, 8000.0);
        obj3.display();
    }
}
